{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit DirectShowManager;

interface

uses Classes, DirectShowAudio, DirectShowRadio, DirectShowVideo, DirectShowDVD;

type
  TAHMDSVideoError = (dvOkay, dvNoDirectShow, dvNoVMR, dvMissingCodec, dvBrokenCodec);
  TAHMDSAudioError = (daOkay, daMissingWMP, daUnknown);

type
  TAHMDirectShowManager = class
  private
    FStreams: TList;
    FLastVideoError: TAHMDSVideoError;
    FLastAudioError: TAHMDSAudioError;
    FCustomAudioRenderer: TAHMDirectShowAudio;
    FCustomVideoRenderer: TAHMDirectShowVideo;
    FCustomDVDRenderer: TAHMDirectShowDVD;
  public
    constructor Create;
    destructor Destroy; override;
    property DirectShowAudioRenderer: TAHMDirectShowAudio read FCustomAudioRenderer;
    property DirectShowVideoRenderer: TAHMDirectShowVideo read FCustomVideoRenderer;
    property DirectShowDVDRenderer: TAHMDirectShowDVD read FCustomDVDRenderer;
    property LastVideoError: TAHMDSVideoError read FLastVideoError write FLastVideoError;
    property LastAudioError: TAHMDSAudioError read FLastAudioError write FLastAudioError;
    function PlayDVD: Boolean;
    function PlayVideo(const Filename: WideString): Boolean;
    function PlayAudio(const Filename: WideString): Boolean;
    function PlayRadio(const Filename: WideString): Boolean;
    procedure StopDVD;
    procedure StopVideo;
    procedure StopAudio;
    procedure ReleaseResources;
    procedure ReleaseStreams;
    procedure ShutDown;
    procedure OnLostDevice;
    procedure OnResetDevice;
  end;

var
  DirectShowInstance: TAHMDirectShowManager;

implementation

constructor TAHMDirectShowManager.Create;
begin
  inherited;

  FStreams := TList.Create;
end;

destructor TAHMDirectShowManager.Destroy;
begin
  ReleaseStreams;
  FStreams.Free;

  inherited;
end;

procedure TAHMDirectShowManager.ReleaseResources;
begin

end;

procedure TAHMDirectShowManager.ReleaseStreams;
begin

end;

function TAHMDirectShowManager.PlayDVD: Boolean;
begin
  // Set default result - failed to play DVD
  Result := False;

  // If we're already playing then don't allocate another stream
  if Assigned(FCustomDVDRenderer) then Exit;

  // Create and configure our custom DVD renderer object
  FCustomDVDRenderer := TAHMDirectShowDVD.Create;

  // Okay, initialise the renderer and play the DVD
  if FCustomDVDRenderer.Initialise then
  begin
    FCustomDVDRenderer.Play;
    Result := True;
  end
  else
  begin
    FCustomDVDRenderer.Release;
    FCustomDVDRenderer := nil;
  end;
end;

function TAHMDirectShowManager.PlayVideo(const Filename: WideString): Boolean;
begin
  // Set default result - failed to play video
  Result := False;

  // If we're already playing then don't allocate another stream
  if Assigned(FCustomVideoRenderer) then Exit;

  // Create and configure our custom VMR allocator presenter object
  FCustomVideoRenderer := TAHMDirectShowVideo.Create;
  FCustomVideoRenderer.Filename := Filename;

  // Okay, initialise the allocator presenter and play the file
  if FCustomVideoRenderer.Initialise then
  begin
    FCustomVideoRenderer.Play;
    Result := True;
  end
  else
  begin
    FCustomVideoRenderer.Release;
    FCustomVideoRenderer := nil;
  end;
end;

function TAHMDirectShowManager.PlayAudio(const Filename: WideString): Boolean;
begin
  // Set default result - failed to play audio
  Result := False;

  // If we're already playing then don't allocate another stream
  if Assigned(FCustomAudioRenderer) then Exit;

  // Create and configure our custom audio renderer
  FCustomAudioRenderer := TAHMDirectShowAudio.Create;
  FCustomAudioRenderer.Filename := Filename;

  // Okay, initialise the graph and play the file
  if FCustomAudioRenderer.Initialise then
  begin
    FCustomAudioRenderer.Play;
    Result := True;
  end
  else
  begin
    FCustomAudioRenderer.Release;
    FCustomAudioRenderer := nil;
  end;
end;

function TAHMDirectShowManager.PlayRadio(const Filename: WideString): Boolean;
begin
  // Set default result - failed to play radio
  Result := False;

  // If we're already playing then don't allocate another stream
  if Assigned(FCustomAudioRenderer) then Exit;

  // Create and configure our custom radio renderer
  FCustomAudioRenderer := TAHMDirectShowRadio.Create;
  FCustomAudioRenderer.Filename := Filename;

  // Okay, initialise the graph and play the file
  if FCustomAudioRenderer.Initialise then
  begin
    FCustomAudioRenderer.Play;
    Result := True;
  end
  else
  begin
    FCustomAudioRenderer.Release;
    FCustomAudioRenderer := nil;
  end;
end;

procedure TAHMDirectShowManager.StopDVD;
begin
  // Shutdown and free our custom DVD renderer
  if Assigned(FCustomDVDRenderer) then
  begin
    FCustomDVDRenderer.ShutDown;
    FCustomDVDRenderer.Release;
    FCustomDVDRenderer := nil;
  end;
end;

procedure TAHMDirectShowManager.StopVideo;
begin
  // Shutdown and free our custom video renderer
  if Assigned(FCustomVideoRenderer) then
  begin
    FCustomVideoRenderer.ShutDown;
    FCustomVideoRenderer.Release;
    FCustomVideoRenderer := nil;
  end;
end;

procedure TAHMDirectShowManager.StopAudio;
begin
  // Shutdown and free our custom audio renderer
  if Assigned(FCustomAudioRenderer) then
  begin
    FCustomAudioRenderer.ShutDown;
    FCustomAudioRenderer.Release;
    FCustomAudioRenderer := nil;
  end;
end;

procedure TAHMDirectShowManager.ShutDown;
begin
  // Ensure video & audio is stopped
  StopDVD;
  StopVideo;
  StopAudio;
end;

procedure TAHMDirectShowManager.OnLostDevice;
begin
  // Release VMR9 resources held in DEFAULT memory pool
  if Assigned(FCustomVideoRenderer) then FCustomVideoRenderer.OnLostDevice;
  if Assigned(FCustomDVDRenderer) then FCustomDVDRenderer.OnLostDevice;
end;

procedure TAHMDirectShowManager.OnResetDevice;
begin
  // Reallocate VMR9 resources if needed
  if Assigned(FCustomVideoRenderer) then FCustomVideoRenderer.OnResetDevice;
  if Assigned(FCustomDVDRenderer) then FCustomDVDRenderer.OnResetDevice;
end;

end.
